package com.specialized.passenger.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.remote.response.Response;
import com.specialized.common.core.domain.MapAddr;
import com.specialized.common.core.domain.Result;
import com.specialized.common.core.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.specialized.common.log.annotation.Log;
import com.specialized.common.log.enums.BusinessType;
import com.specialized.common.security.annotation.RequiresPermissions;
import com.specialized.passenger.domain.ChatMsg;
import com.specialized.passenger.service.IChatMsgService;
import com.specialized.common.core.web.controller.BaseController;
import com.specialized.common.core.utils.poi.ExcelUtil;
import org.springframework.web.servlet.ModelAndView;

/**
 * 聊天记录Controller
 *
 * @author djh
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/ChatMsg")
public class ChatMsgController extends BaseController {
    @Autowired
    private IChatMsgService chatMsgService;

/**
 * 查询聊天记录列表
 */
@RequiresPermissions("passenger:ChatMsg:list")
@GetMapping("/list")
    public Result list(ChatMsg chatMsg) {
        List<ChatMsg> list = chatMsgService.selectChatMsgList(chatMsg);
        return getDataTable(list);
    }

    /**
     * 获取聊天记录详细信息
     */
    @RequiresPermissions("passenger:ChatMsg:query")
    @GetMapping(value = "/{chatId}")
    public Result getInfo(@PathVariable("chatId") Long chatId) {
        return success(chatMsgService.selectChatMsgByChatId(chatId));
    }

    /**
     * 新增聊天记录
     */
    @RequiresPermissions("passenger:ChatMsg:add")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody ChatMsg chatMsg) {
        return toAjax(chatMsgService.insertChatMsg(chatMsg));
    }

    /**
     * 修改聊天记录
     */
    @RequiresPermissions("passenger:ChatMsg:edit")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody ChatMsg chatMsg) {
        return toAjax(chatMsgService.updateChatMsg(chatMsg));
    }

    /**
     * 删除聊天记录*
     */
    @RequiresPermissions("passenger:ChatMsg:remove")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chatIds}")
    public Result remove(@PathVariable Long[] chatIds) {
        return toAjax(chatMsgService.deleteChatMsgByChatIds(chatIds));
    }

    /**
     * 打开消息发送页面
     */
    @RequestMapping("/webSocketPage")
    public ModelAndView page(){
        return new ModelAndView("index");
    }

    /**
     * 获取在线司机
     */
    @RequestMapping("/members")
    public Response members(){
        return chatMsgService.members();
    }

    @GetMapping("findAddr")
    public Result<MapAddr> findAddr(@RequestParam("addr") String addr){
        String resultAddr = MapUtil.findAddr(addr);
        MapAddr mapAddr = JSONObject.parseObject(resultAddr, MapAddr.class);

        Calendar instance1 = Calendar.getInstance();
        instance1.set(Calendar.HOUR_OF_DAY,23);
        instance1.set(Calendar.MINUTE,0);
        instance1.set(Calendar.SECOND,0);
        Date time1 = instance1.getTime();
        System.out.println(time1);

        Calendar instance2 = Calendar.getInstance();
        instance2.set(Calendar.HOUR_OF_DAY,06);
        instance2.set(Calendar.DAY_OF_MONTH,(new Date().getDate()+1));
        instance2.set(Calendar.MINUTE,0);
        instance2.set(Calendar.SECOND,0);
        Date time2 = instance2.getTime();
        System.out.println(time2);

        boolean in = DateUtil.isIn(new Date(), time1, time2);
        System.out.println("判断结果为: "+ in);

        return Result.success(mapAddr);
    }
}
